/**
 * @author      CPT 236 - 001
 *
 * @version     1.0 Final (maybe)
 */

package battleship.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import battleship.Game;
import battleship.model.Shot;

		/**
		* Gameplay stage. This is where the magic happens.
		*
		* This creates the gameplay GUI. It includes two 100 button girds (the actual game stage),
		* it sets the color of the cells to blue (You know like water), and also includes the
		* win/loss conditions.
		*/

public class GamePlay extends JFrame {

	public JButton[][] player = new JButton[10][10];
	public JButton[][] computer = new JButton[10][10];
	//public JLabel shipsRemaining = new JLabel("           Enemy Ships Remaining: 5");
	private Game game;
	
	public GamePlay(){
		
		super("Battleship: In Game");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(400, 800);
		setVisible(true);
		
		JPanel computerPanel = new JPanel(new GridLayout (10,10));
		JPanel playerPanel = new JPanel(new GridLayout (10,10));
		JPanel gridHolder = new JPanel(new GridLayout (2,1));
		game = new Game();
		
		int counter = -1;
		
		for(int y = 0; y < 10; y++)
		{
			for(int x = 0; x < 10; x++)
			{
				counter++;
				computer[x][y]= new JButton("");
				computer[x][y].setBackground(Color.BLUE);
				computer[x][y].setActionCommand("" + counter);
				computer[x][y].addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent aE) {
						int x = Integer.parseInt(aE.getActionCommand()) / 10;
						int y = Integer.parseInt(aE.getActionCommand()) % 10;
						processTurn(x, y, aE);
					}
				});
				computerPanel.add(computer[x][y]);
			}
		}
		
		for(int y = 0; y < 10; y++)
		{
			for(int x = 0; x < 10; x++)
			{
				player[x][y]= new JButton("");
				player[x][y].setBackground(game.setColor(x, y));
				playerPanel.add(player[x][y]);
			}
		}
		
		gridHolder.add(computerPanel);
		gridHolder.add(playerPanel);
		add(gridHolder, BorderLayout.CENTER);
	}
	
   
		/**
		* Method for a message box that displays hit or miss, currently removed due to
		* it being annoying and cumbersome for the user to need to dismiss it.
		*/
	public void AttackButton(boolean hit) {
		if(hit){
			JOptionPane.showMessageDialog(this,"Hit!",
	                "Sweet", JOptionPane.PLAIN_MESSAGE);
		} else {
			JOptionPane.showMessageDialog(this,"Miss!",
	                "Booo", JOptionPane.PLAIN_MESSAGE);
		}
		
	}
	
   
		/**
		* Method that decides whether the player won of loss. Based on whether
		* all of the players ships are sunk or not. If not and the coimputers are, the Player wins.
		* If the other case then the Computer wins. 
		*/
	public void processTurn(int x, int y, ActionEvent aE) {
		game.processPlayerTurn(x, y, aE);
		if(game.computerSunk()) {
			new GameOverMenu("You");
			setVisible(false);
		} else {
			ComputerAttack(game.processComputerTurn());
			game.playerSunk();
			if(game.playerSunk()) {
				new GameOverMenu("The Computer");
				setVisible(false);
			}
		}
	}
   
  	/**
		* Method that changes the color of attack squares. White if a miss, Red if a hit.
		*/
	public void ComputerAttack(Shot shot) {
		String Ship = game.getShip(shot);
		if(Ship != ""){
			JOptionPane.showMessageDialog(this,"Your " + Ship + " was hit!",
	                "HIT!", JOptionPane.PLAIN_MESSAGE);
			player[shot.x][shot.y].setBackground(Color.RED);
		} else {
			JOptionPane.showMessageDialog(this,"The Computer missed!",
	                "MISS!", JOptionPane.PLAIN_MESSAGE);
			player[shot.x][shot.y].setBackground(Color.WHITE);
		}
		
	}
	
}